package com.shunda.ysdgj.manager.controller.goods;

import com.shunda.ysdgj.manager.service.goods.GoodsCategoryService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.entity.goods.GoodsCategory;
import com.shunda.ysdgj.model.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "商品分类接口")
@RestController
@RequestMapping("/api/v1/admin/goods/category")
@RequiredArgsConstructor
public class GoodsCategoryController {

    private final GoodsCategoryService goodsCategoryService;

    @Operation(summary = "根据父ID获取商品分类")
    @GetMapping("list/{parentId}")
    public ResultVo list(@PathVariable Long parentId) {
        return ResultVo.success(goodsCategoryService.findByParentId(parentId));
    }

    @Operation(summary = "添加商品分类")
    @PostMapping("add")
    public ResultVo add(@RequestBody GoodsCategory goodsCategory) {
        if (!goodsCategoryService.add(goodsCategory)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "修改商品分类")
    @PostMapping("modify")
    public ResultVo modify(@RequestBody GoodsCategory goodsCategory) {
        if (!goodsCategoryService.modify(goodsCategory)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "删除商品分类")
    @PostMapping("delete")
    public ResultVo delete(@RequestBody IdsDto idsDto) {
        if (!goodsCategoryService.deleteByIds(idsDto)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

}
